package com.coolwar.design.proxy.dynamic.mybatis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultSqlSession implements SqlSession {

    @Override
    public <T> T select(String statement, Object parameter) {
        System.out.println("执行了查询：" + statement);
        Map<String, Object> objectObjectHashMap = new HashMap<>(4);
        objectObjectHashMap.put("name", "wen");
        objectObjectHashMap.put("age", "123");
        objectObjectHashMap.put("id", "id1");
        objectObjectHashMap.put("sex", "1");
        objectObjectHashMap.put("number", "11");
        return (T) objectObjectHashMap;
    }

    @Override
    public <E> List<E> selectList(String statement, Object parameter) {
        System.out.println("执行了查询多条：" + statement);
        Map<String, Object> objectObjectHashMap = new HashMap<>(4);
        objectObjectHashMap.put("name", "wen");
        objectObjectHashMap.put("age", "123");
        objectObjectHashMap.put("id", "id1");
        objectObjectHashMap.put("sex", "1");
        objectObjectHashMap.put("number", "11");
        List<Object> result = new ArrayList<>();
        result.add(objectObjectHashMap);
        return (List<E>) result;
    }

    @Override
    public int update(String statement, Object parameter) {
        System.out.println("执行了更新：" + statement);
        return 1;
    }

    @Override
    public int delete(String statement, Object parameter) {
        System.out.println("执行了删除：" + statement);
        return 1;
    }

    @Override
    public int insert(String statement, Object parameter) {
        System.out.println("执行了插入：" + statement);
        return 1;
    }

    Map<String, String> map = null;

    @Override
    public Map<String, String> getMapperConfiguration() {
        if (map == null) {
            // 读取xml文件，加载
            map = new HashMap<>(2);
            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IStudentDao.save", "insert into student (id, name, age) values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})");
            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IStudentDao.select", "select * from table where id = #{id}");
            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IStudentDao.search", "select * from table where name = #{name}");

            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IGradeDao.save", "insert into student (id, name, age) values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})");
            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IGradeDao.select", "select * from table where id = #{id}");
            map.put("com.coolwar.design.proxy.dynamic.mybatis.dao.IGradeDao.search", "select * from table where name = #{name}");
        }
        return map;
    }
}
